package de.tudresden.st.cbse.servlet;

import java.io.IOException;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.tudresden.st.cbse.util.RemoteLookup;

@WebServlet({"/calendar"})
public class CalendarServlet extends HttpServlet {
    private static final long serialVersionUID = 8895301499413413624L;

    private boolean validateAuthentication(HttpServletRequest req) {
        return req.getSession(true).getAttribute("currentSessionUser") != null;
    }
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (!validateAuthentication(req)) {
            resp.sendRedirect("index.jsp");
            return;
        }
        
        RequestDispatcher resultView = req.getRequestDispatcher("calendar.jsp");
        try {
            req.getSession(true).setAttribute("appointments", RemoteLookup.getCalendarManager().getCalendarForUser((String) req.getSession(true).getAttribute("currentSessionUser")).get());
        } catch (NamingException e) {
            e.printStackTrace();
        }
        resultView.forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
    
    
}
